package de.wittekind.bundesjugendspiele.server.impl;

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import de.wittekind.bundesjugendspiele.server.DatabaseConnectionManager;
import de.wittekind.bundesjugendspiele.server.interfaces.Discipline;
import de.wittekind.bundesjugendspiele.server.interfaces.Try;
import de.wittekind.bundesjugendspiele.server.interfaces.Unit;

public class SQLTry implements Try {

	private int id;
	private double value;
	private Unit unit;
	private Discipline discipline;

	public SQLTry(int id) throws IOException {
		this.id = id;
		try {
			Connection c = DatabaseConnectionManager.getConnection();
			Statement s = c.createStatement();
			String sql = "select disziplin, wert, einheit from versuch where id="
					+ id;
			ResultSet rs = s.executeQuery(sql);
			rs.next();
			discipline = Discipline.values()[rs.getInt(1)];
			value = rs.getDouble(2);
			unit = Unit.values()[rs.getInt(3)];
			s.close();
		} catch (SQLException ex) {
			ex.printStackTrace();
		}
	}

	public SQLTry(int id, double value, Unit unit, Discipline discipline)
			throws SQLException {
		super();
		this.id = id;
		this.value = value;
		this.unit = unit;
		this.discipline = discipline;
	}

	@Override
	public int getID() throws IOException{
		return id;
	}

	@Override
	public double getValue() throws IOException {
		return value;
	}

	@Override
	public Unit getUnit() throws IOException {
		return unit;
	}

	@Override
	public Discipline getDiscipline() throws IOException {
		return discipline;
	}

	@Override
	public void setValue(double value) throws IOException {
		this.value = value;
		try {
			Connection c = DatabaseConnectionManager.getConnection();
			Statement s = c.createStatement();
			String sql = String
					.format("update versuch set wert=%s, tswert=CURRENT_TIMESTAMP where id=%s",
							value, id);
			s.executeUpdate(sql);
			s.close();
		} catch (SQLException ex) {
			throw new IOException(ex.getMessage());
		}
	}

	@Override
	public void setUnit(Unit unit) throws IOException {
		this.unit = unit;
		try {
			Connection c = DatabaseConnectionManager.getConnection();
			Statement s = c.createStatement();
			String sql = String.format(
					"update versuch set einheit=%s where id=%s",
					unit.ordinal(), id);
			s.executeUpdate(sql);
			s.close();
		} catch (SQLException ex) {
			throw new IOException(ex.getMessage());
		}
	}

	@Override
	public void setDiscipline(Discipline discipline) throws IOException {
		this.discipline = discipline;
		try {
			Connection c = DatabaseConnectionManager.getConnection();
			Statement s = c.createStatement();
			String sql = String.format(
					"update versuch set disziplin=%s where id=%s",
					discipline.ordinal(), id);
			s.executeUpdate(sql);
			s.close();
		} catch (SQLException ex) {
			throw new IOException(ex.getMessage());
		}

	}

}
